﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Videos2Links
{
    public class CSpecialFolder
    {
        public List<VideoClipNameHelper.VideoFile> SourceFilesExpanded { get; private set; }
        public string DestinationRoot { get; private set; }
        //
        public string DestinationDir { get; private set; }
        public List<string> errList { get; private set; }
        public List<VideoClipNameHelper.VideoFile> foundClips { get; private set; }
        public Dictionary<string, string> configData { get; private set; }
        public List<string> clipNames { get; private set; }
        //
        //================================================================
        public CSpecialFolder(List<VideoClipNameHelper.VideoFile> sourceFilesExpanded, string destinationRoot)
        {
            this.SourceFilesExpanded = sourceFilesExpanded;
            this.DestinationRoot = destinationRoot;
            errList = new List<string>();
            foundClips = new List<VideoClipNameHelper.VideoFile>();
            configData = new Dictionary<string, string>();
            clipNames = new List<string>();
        }
        //================================================================
        public void ReadConfigFile(string specialFileNameFullPathNeme)
        {
            configData.Clear();
            clipNames.Clear();
            foundClips.Clear();
            errList.Clear();
            //
            DestinationDir = System.IO.Path.Combine(DestinationRoot, "@"+System.IO.Path.GetFileNameWithoutExtension(specialFileNameFullPathNeme));
            //
            try
            {
                readSpecialFile(specialFileNameFullPathNeme, clipNames, configData);
                //
                configData["@SourceFile".ToLower()] = System.IO.Path.GetFileName(specialFileNameFullPathNeme);
                //
                if ("" != VideoClipNameHelper.GeneralConfig.GetValueString(configData, "DestinationDir"))
                {
                    DestinationDir = System.IO.Path.Combine(DestinationRoot, "@" + System.IO.Path.GetFileNameWithoutExtension(VideoClipNameHelper.GeneralConfig.GetValueString(configData, "DestinationDir")));
                }
                //
                //
                foreach (string f0 in clipNames)
                {
                    string f = f0.Trim();
                    //
                    if(f.StartsWith("Artist:", StringComparison.InvariantCultureIgnoreCase))
                    {
                        string artist = f.Substring(7).Trim();
                        //
                        System.Collections.Specialized.NameValueCollection nv = new System.Collections.Specialized.NameValueCollection();
                        artist = VideoClipNameHelper.OptionParser.GetOptions(artist, nv);
                        //
                        foreach (VideoClipNameHelper.VideoFile vf in SourceFilesExpanded.FindAll(x => x.vcd.Artist == artist))
                        {
                            foundClips.Add(vf);
                        }
                        foreach (string title in VideoClipNameHelper.OptionParser.GetValuesSafe(nv, "EXCLUDE"))
                        {
                            foundClips.RemoveAll(x => x.vcd.Title.ToUpperInvariant() == title.ToUpperInvariant());
                        }
                        continue;
                    }
                    if (f.StartsWith("Title:", StringComparison.InvariantCultureIgnoreCase))
                    {
                        string title = f.Substring(6).Trim();
                        //
                        System.Collections.Specialized.NameValueCollection nv = new System.Collections.Specialized.NameValueCollection();
                        title = VideoClipNameHelper.OptionParser.GetOptions(title, nv);
                        //
                        foreach (VideoClipNameHelper.VideoFile vf in SourceFilesExpanded.FindAll(x => x.vcd.Title == title))
                        {
                            foundClips.Add(vf);
                        }
                        foreach (string artist in VideoClipNameHelper.OptionParser.GetValuesSafe(nv, "EXCLUDE"))
                        {
                            foundClips.RemoveAll(x => x.vcd.Artist.ToUpperInvariant() == artist.ToUpperInvariant());
                        }
                    continue;
                    }
                    //
                    VideoClipNameHelper.VideoClipData vc = new VideoClipNameHelper.VideoClipData();
                    /*
                    if("" == VideoClipNameHelper.VideoClipData.Parse(System.IO.Path.GetFileNameWithoutExtension(f), vc))
                    {
                        //Console.WriteLine("\tclipName:[{0}]", f);
                        VideoClipNameHelper.VideoFile found = SourceFilesExpanded.FirstOrDefault(x => x.vcd == vc);
                        //
                        //
                        if (null != found)
                        {
                            foundClips.Add(found);
                        }
                        else
                        {
                            errList.Add(string.Format("ReadConfigFile({0}) Clip not found [{1}]", System.IO.Path.GetFileName(specialFileNameFullPathNeme), System.IO.Path.GetFileName(f)));
                        }
                    }
                    else
                    {
                        errList.Add(string.Format("ReadConfigFile({0}) Parsing clip name error [{1}]", System.IO.Path.GetFileName(specialFileNameFullPathNeme), System.IO.Path.GetFileName(f)));
                    }
                    */
                    //-------------------------------------------------------------
                    //
                    VideoClipNameHelper.VideoClipData.PARSE_RESULT pr = VideoClipNameHelper.VideoClipData.ParseWeak(f, vc);
                    switch(pr)
                    {
                        case VideoClipNameHelper.VideoClipData.PARSE_RESULT.Full:
                            VideoClipNameHelper.VideoFile found = SourceFilesExpanded.FirstOrDefault(x => x.vcd == vc);
                            //
                            if (null != found)
                            {
                                foundClips.Add(found);
                            }
                            else
                            {
                                errList.Add(string.Format("ReadConfigFile({0}) Clip not found [{1}]", System.IO.Path.GetFileName(specialFileNameFullPathNeme), System.IO.Path.GetFileName(f)));
                            }
                            break;
                        case VideoClipNameHelper.VideoClipData.PARSE_RESULT.Weak:
                            // case???
                            //VideoClipNameHelper.VideoFile foundWeak = SourceFilesExpanded.FirstOrDefault(x => x.vcd.Artist == vc.Artist && x.vcd.Title == vc.Title);
                            VideoClipNameHelper.VideoFile foundWeak = SourceFilesExpanded.FirstOrDefault(x => x.vcd.IsEqWeak(vc));
                            //
                            if (null != foundWeak)
                            {
                                foundClips.Add(foundWeak);
                            }
                            else
                            {
                                errList.Add(string.Format("ReadConfigFile({0}) Clip not found [{1}]", System.IO.Path.GetFileName(specialFileNameFullPathNeme), System.IO.Path.GetFileName(f)));
                            }
                            break;
                        case VideoClipNameHelper.VideoClipData.PARSE_RESULT.Invlid:
                            errList.Add(string.Format("ReadConfigFile({0}) Parsing clip name error [{1}]", System.IO.Path.GetFileName(specialFileNameFullPathNeme), System.IO.Path.GetFileName(f)));
                            break;
                    }
                    //
                    //-------------------------------------------------------------
                }
                //
            }
            catch(Exception e)
            {
                configData.Clear();
                string eMsg = string.Format("ERROR ReadConfigFile {0},{1},({2}),{3} specialFileNameFullPathNeme=[{4}]", e.Message, e.Source, e.StackTrace, e.InnerException, specialFileNameFullPathNeme);
                VideoClipNameHelper.CLogger.Write(eMsg);
                errList.Add(eMsg);
                Console.WriteLine(eMsg);
            }
            //
            return ;
        }
        //================================================================
        public void readSpecialFile(string specialFileNameFullPathNeme, List<string> clipNames, Dictionary<string, string> configData)
        {
            //
            try
            {
                if (!System.IO.File.Exists(specialFileNameFullPathNeme))
                {
                    configData.Clear();
                    throw new ArgumentException(string.Format("ReadConfigFile: !File.Exists({0})", specialFileNameFullPathNeme));
                }
                VideoClipNameHelper.GeneralConfig.ParseConfigLines(System.IO.File.ReadLines(specialFileNameFullPathNeme, Encoding.UTF8).ToList<string>(), configData, clipNames);
                //
            }
            catch (Exception e)
            {
                configData.Clear();
                string eMsg = string.Format("ERROR ReadConfigFile {0},{1},({2}),{3} specialFileNameFullPathNeme=[{4}]", e.Message, e.Source, e.StackTrace, e.InnerException, specialFileNameFullPathNeme);
            }
            //
            return;
        }
        //================================================================
        public override string ToString()
        {
            string s = "";
            s += string.Format("DestinationRoot=[{0}[{1}", DestinationRoot, Environment.NewLine);
            s += string.Format("DestinationDir=[{0}[{1}", DestinationDir, Environment.NewLine);
            //
            s += ("configData" + Environment.NewLine);
            foreach (KeyValuePair<string, string> x in configData)
            {
                s += string.Format("[{0}]=[{1}]{2}", x.Key, x.Value, Environment.NewLine);
            }
            //
            s += ("clipNames" + Environment.NewLine);
            foreach (string x in clipNames)
            {
                s += string.Format("[{0}]{1}", x, Environment.NewLine);
            }
            s += Environment.NewLine;
            //
            return s;
        }
        //================================================================
        public void Dump(string fileName)
        {
            try
            {
                System.IO.File.WriteAllText(fileName, this.ToString(), Encoding.UTF8);
            }
            catch { }
        //public List<VideoClipNameHelper.VideoFile> foundClips { get; private set; }
        //public Dictionary<string, string> configData { get; private set; }

    }
    //================================================================
}//class
}//namespace
